import Vue from "vue";
import VueRouter from "vue-router";
import router_index   from  './router_home';
import store  from '../store';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';

Vue.use(VueRouter);



const routes = [
      ...router_index
];

const router = new VueRouter({
  mode: "hash",           // hash 加#       history   /
  base: process.env.BASE_URL,
  routes
});

NProgress.inc(0.2)
NProgress.configure({ easing: 'ease', speed: 500, showSpinner: false })
//  路由守卫配置
router.beforeEach((to, from, next) => {
  NProgress.start()
  const store_   =localStorage.getItem('store')?JSON.parse(localStorage.getItem('store')).token||'':'',
  buttonPermission = store.state.buttonPermission||{};
  let {path,name}=to||{};
  buttonPermission.hasOwnProperty(path) ? to.query.check=buttonPermission[path] : to.query.check={};
  if(to.name=='login'){
    next();
  }else{
      if(store_&&store_!=null&&store_!=undefined){
           next()
      }else{
           next('/login')
      } 
  }
})
router.afterEach(() => {
  NProgress.done()
})

export default router;
